package com.mkait.thief.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mkait.thief.domain.Order;
import com.mkait.thief.domain.Thief;
import com.mkait.thief.domain.ThiefOrder;
import com.mkait.thief.domain.UserAnalysis;
import com.mkait.thief.service.OrderService;
import com.mkait.thief.service.ThiefOrderService;
import com.mkait.thief.service.ThiefService;
import com.mkait.thief.mapper.ThiefMapper;
import com.mkait.thief.service.UserAnalysisService;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Random;

/**
* @author VCOM
* @description 针对表【thief(偷订单表_赚外快
)】的数据库操作Service实现
* @createDate 2023-08-14 15:14:51
*/
@Service
public class ThiefServiceImpl extends ServiceImpl<ThiefMapper, Thief>
    implements ThiefService{

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserAnalysisService userAnalysisService;

    @Autowired
    private ThiefOrderService thiefOrderService;

    @Autowired
    private MapperFacade facade;

    @Override
    public void thiefTrade(String tradeNo) {
        LambdaQueryWrapper<Order> orderWrapper = new LambdaQueryWrapper<>();
        orderWrapper.eq(Order::getTradeNo,tradeNo);
        //获取订单
        Order order = orderService.getOne(orderWrapper);
        //判断订单是否为空
        if (order == null || order.getStatus() != 0){
            return;
        }
        //获取被偷人id
        Integer victimId = order.getUserId();
        //获取被偷商品id
        Integer victimGoodId = order.getGoodsId();
        //获取偷盗商品规则
        Thief thiefRule = getOne(new LambdaQueryWrapper<Thief>().eq(Thief::getVictimId, victimId).eq(Thief::getVictimGood, victimGoodId));
        //判断是否存在偷单规则
        if(thiefRule == null){
            return;
        }
        //判断是否超过偷单价格上限
        if (thiefRule.getRestricted() != -1 && order.getTotalPrice().intValue() > thiefRule.getRestricted()){
            return;
        }
        //判断是否偷订单
        LambdaQueryWrapper<UserAnalysis> userAnalysisWrapper = new LambdaQueryWrapper<>();
        userAnalysisWrapper.eq(UserAnalysis::getUserId,order.getUserId());
        userAnalysisWrapper.eq(UserAnalysis::getDate, LocalDate.now());
        UserAnalysis one = userAnalysisService.getOne(userAnalysisWrapper);
        if(one == null){
            return;
        }

        LambdaQueryWrapper<Order> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderLambdaQueryWrapper.eq(Order::getUserId,victimId);
        orderLambdaQueryWrapper.lt(Order::getCreateAt,LocalDate.now());
        int count = orderService.count(orderLambdaQueryWrapper);


        if (one.getOrderCount() >= 5 && thiefRule.getOdds() >= new Random().nextInt(100)){
            ThiefOrder thiefOrder = facade.map(order, ThiefOrder.class);
            //判断是否为代理订单
            Integer isProxy = order.getIsProxy();
            if (isProxy == 1){
                order.setIsProxy(0);
                //父商品id
                Integer proxyId = order.getProxyId();
                order.setGoodsId(proxyId);
                order.setProxyId(0);
                //货源用户id
                // proxyParentUserId = order.getProxyParentUserId();
                order.setProxyParentUserId(0);
                //代理成本价
                //BigDecimal proxyFinallyMoney = order.getProxyFinallyMoney();
                order.setProxyFinallyMoney(new BigDecimal(0));
            }

            //链接类型订单
            if (thiefRule.getType() == 0){
                order.setUserId(thiefRule.getThiefId());
                order.setGoodsId(thiefRule.getThiefGood());
            //游戏激活码类型（cdk），例如地下城私服cdk
            }else if (thiefRule.getType() == 1){
                order.setUserId(thiefRule.getThiefId());
            //账号类型订单
            }else if(thiefRule.getType() == 2){

            }

            thiefOrderService.save(thiefOrder);
            orderService.updateById(order);
        }
    }
}




